package com.ty.gpiotest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText mEdit;
	private TextView mGetValue;
	
	private Button mBtnDirIn;
    private Button mBtnDirOut;
    private Button mBtnDataHigh;
    private Button mBtnDataLow;
    private Button mBtnGetData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.d("hcj.gpio", "onCreate");
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.gpio);
		
		mEdit = (EditText)findViewById(R.id.GPIO_Edit_Value);
		mBtnDirIn = (Button) findViewById(R.id.GPIO_Direction_In);
        mBtnDirOut = (Button) findViewById(R.id.GPIO_Direction_Out);
        mBtnDataHigh = (Button) findViewById(R.id.GPIO_Data_High);
        mBtnDataLow = (Button) findViewById(R.id.GPIO_Data_Low);
        
        mBtnGetData = (Button) findViewById(R.id.GPIO_Getin_btn);
        mGetValue = (TextView) findViewById(R.id.GPIO_getin_text);
        
        mBtnDirIn.setOnClickListener(mOnClickListener);
        mBtnDirOut.setOnClickListener(mOnClickListener);
        mBtnDataHigh.setOnClickListener(mOnClickListener);
        mBtnDataLow.setOnClickListener(mOnClickListener);
        mBtnGetData.setOnClickListener(mOnClickListener);
	}
	
	private View.OnClickListener mOnClickListener = new View.OnClickListener(){

		@Override
		public void onClick(View view) {
			String editString = mEdit.getText().toString();
	        if (null == editString || editString.equals("")
	                || editString.length() > 4) {
	            Toast.makeText(MainActivity.this, "Please input the value.", Toast.LENGTH_LONG)
	                    .show();
	            return;
	        }
	        int inputValue = Integer.valueOf(mEdit.getText().toString());
	        
	        int ret = -1;
	        int cmd = -1;
			if(view == mBtnDirIn){
				cmd = Gpio.SET_DIR_IN;
				ret = Gpio.Cmd(Gpio.SET_MODE_0, inputValue);
				ret = Gpio.Cmd(Gpio.SET_DIR_IN, inputValue);
			}else if(view == mBtnDirOut){
				cmd = Gpio.SET_DIR_OUT;
				ret = Gpio.Cmd(Gpio.SET_MODE_0, inputValue);
				ret = Gpio.Cmd(Gpio.SET_DIR_OUT, inputValue);
			}else if(view == mBtnDataHigh){
				cmd = Gpio.SET_DATA_HIGH;
				ret = Gpio.Cmd(Gpio.SET_DATA_HIGH, inputValue);
			}else if(view == mBtnDataLow){
				cmd = Gpio.SET_DATA_LOW;
				ret = Gpio.Cmd(Gpio.SET_DATA_LOW, inputValue);
			}else if(view == mBtnGetData){
				cmd = Gpio.GET_DATA_IN;
				ret = Gpio.Cmd(Gpio.GET_DATA_IN, inputValue);
				if(ret != -1){
					mGetValue.setText(String.valueOf(ret));
				}
			}
			Log.d("hcj.gpio", "onClick cmd="+cmd+",ret="+ret);
		}
		
	};
}
